package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import android.widget.EditText;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.CdbInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.DataManager;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IUpdateCdb;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.AwaitDialog;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 作者：Administrator on 2017/7/5 15:17
 * 邮箱：zhanghuaiha@gmail.com
 */

public class UpdateCdbPresenter extends BasePresenter<IUpdateCdb> {


    private String eid;
    private EditText et;
    private TextView tv;
    private EditText remark;
    private String versionName;
    private String id;
    private AwaitDialog selfDialog;

    public UpdateCdbPresenter(BaseActivity context) {
        super(context);
    }

    public void loadContacts(String udid,String alias){
//        manager = new DataManager(mContext);
//        mCompositeSubscription = new CompositeSubscription();
        eid = App.SP.getString("id", "");
        et = getView().getET();
        tv = getView().getTV();
        remark = getView().getRemark();
        versionName = ObtainVersion.getVersionName(mContext);
        getCdb_info(udid,alias);
    }

    //查找设备
    private void getCdb_info(String udid,String alias) {
        String time = System.currentTimeMillis() + "";
        String encry = ObtainEncry.getCdb_info("2", time, versionName, NetWorkCodeInfo.GETCDB_INFO, udid, eid, alias);
        RetrofitFactory.getmInstanse().mApi.getCdb_info("2",time, versionName, NetWorkCodeInfo.GETCDB_INFO, udid, eid,alias,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<CdbInfo>>() {
                    @Override
                    public void success(BaseResponseEntity<CdbInfo> cdbInfoBaseResponseEntity) {
                        if (cdbInfoBaseResponseEntity.code == 200) {
                            CdbInfo data = cdbInfoBaseResponseEntity.data;
                            if (data != null) {
                                CdbInfo.ChargingBean charging = data.getCharging();
                                CdbInfo.StoreBean store = data.getStore();
                                if (charging.getName() != null) {
                                    et.setText((String)charging.getName());
                                }
                                if (charging.getRemark() != null) {
                                    remark.setText((String)charging.getRemark());
                                }

                                if (store.getName() != null) {
                                    tv.setText(store.getName());
                                }
                                id = store.getId();
                            }
                        }else {
                            ToastUtil.showToast(cdbInfoBaseResponseEntity.message);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                    }
                });
    }


    public void loadData(String alias,String udid,String id,String name,String remark) {
        Dialog("正在激活..请等待.");
        String time = System.currentTimeMillis() + "";
        if (id == null || id.equals("")) {
            id = this.id;
        }
        String encry = ObtainEncry.getActivation("2", time, versionName, NetWorkCodeInfo.UPDATE_CDB, udid, eid, alias,name,remark, id);
        RetrofitFactory.getmInstanse().mApi.getUpdate_cdb("2",time,versionName, NetWorkCodeInfo.UPDATE_CDB,udid,eid,alias,name,remark,id,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponseEntity>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        selfDialog.dismiss();
                    }

                    @Override
                    public void onNext(BaseResponseEntity codeInt) {
                        if (codeInt.code == 200) {
                            ToastUtil.showToast("更新成功..");
                            mContext.finish();
                        }else {
                            ToastUtil.showToast(codeInt.message);
                        }
                        selfDialog.dismiss();

                    }
                });
    }

    public void Dialog(String a) {
        selfDialog = new AwaitDialog(mContext);
        selfDialog.setTitle("提示");
        selfDialog.setMessage(a);
        selfDialog.show();
    }
}
